/*
 * This function originally from Stuart Langridge at http://www.kryogenix.org/
 * Heavily updated by Jeff Minard at http://www.creatimation.net/
 * Some stupid modifications to make it work for BIM - BIM Input Method (alt: wasaBi's Input Method)  at http://www.silenceisdefeat.org/~wasabi/
 */
 
 // defaults
 var bimTagStyle = 0;
	// 0 - XHTML, 
	// 1 - HTML, 
	// 2 - bbCode (Modern: IPB/VBB/PunBB i.e. [img]http://my.url.to/image.jpg[/img]), 
	// 3 - bbCode (Old style, depreciated, i.e. [img=http://my.url.to/image.jpg])
 var bimParseSmileys = 0;
	// parse smileys eg ;) :) :(
	// not implemented :(
 var bimPleaseCrashWithHimJs = false;
	// I dunno what this setting means, set it to false, I beg you!

	
// wasabi: okay let's begin
function superTextile(s) {

    var r = s;
	
    // quick tags first
	
	// BIM mods
	var strongTag = ((bimTagStyle == 0) ? 'strong' : 'b'); // XHTML uses strong, other use B
	var emTag = ((bimTagStyle == 0) ? 'em' : 'i'); // idiot
	
	var citeTag = ((bimTagStyle == 0 || bimTagStyle == 1) ? 'cite' : 'b'); // WORKAROUND (X)HTML uses cite, bbCode uses b
	
	var beginTagSym = ((bimTagStyle == 0 || bimTagStyle == 1) ? '<' : '['); 
	var endTagSym = ((bimTagStyle == 0 || bimTagStyle == 1) ? '>' : ']'); 
	
    qtags = [['\\*', strongTag],
             ['\\?\\?', citeTag],
             ['\\+', 'ins'],  //fixed
             ['~', 'sub'],   
             ['\\^', 'sup'], // Jeff
             ['@', 'code']];
    for (var i=0;i<qtags.length;i++) {
        ttag = qtags[i][0]; htag = qtags[i][1];
        re = new RegExp(ttag+'\\b(.+?)\\b'+ttag,'g');
        r = r.replace(re,beginTagSym+htag+endTagSym+'$1'+beginTagSym+'/'+htag+endTagSym);
    }
	
    // underscores count as part of a word, so do them separately
    re = new RegExp('\\b_(.+?)_\\b','g');
    r = r.replace(re,beginTagSym+emTag+endTagSym+'$1'+beginTagSym+'/'+emTag+endTagSym);
	
	//jeff: so do dashes
    re = new RegExp('[\s\n]-(.+?)-[\s\n]','g');
    r = r.replace(re,beginTagSym+'del'+endTagSym+'$1'+beginTagSym+'/del'+endTagSym);

    // links
	// BIM: (X) HTML uses <a>, bbCode uses [URL]
	// almost differrent
	if(bimTagStyle == 0 || bimTagStyle == 1) {
	    re = new RegExp('"\\b(.+?)\\(\\b(.+?)\\b\\)":([^\\s]+)','g');
	    r = r.replace(re,beginTagSym+'a href="$3" title="$2"'+endTagSym+'$1'+beginTagSym+'/a'+endTagSym);
	    re = new RegExp('"\\b(.+?)\\b":([^\\s]+)','g');
	    r = r.replace(re,beginTagSym+'a href="$2"'+endTagSym+'$1'+beginTagSym+'/a'+endTagSym);
	} else {
		// BIM: note: almost bb Systems auto parse the http://my.url.com/ string to a link, so we don't need to care about it
		// at least, in this stage of development
	    re = new RegExp('"\\b(.+?)\\b":([^\\s]+)','g');
	    r = r.replace(re,beginTagSym+'url=$2'+endTagSym+'$1'+beginTagSym+'/url'+endTagSym);
	}

    // images
	// BIM: (X) HTML uses alt=, bbCode not applicable
	if(bimTagStyle == 0 || bimTagStyle == 1) {
	    re = new RegExp('!\\b(.+?)\\(\\b(.+?)\\b\\)!','g');
		r = r.replace(re,beginTagSym+'img src="$1" alt="$2"'+((bimTagStyle == 0)?' /':'')+endTagSym); // self-close tag style ;)
	}
	// BIM: both, a bit confusing
    re = new RegExp('!\\b(.+?)\\b!','g');
	if(bimTagStyle == 0 || bimTagStyle == 1) {
		r = r.replace(re,beginTagSym+'img src="$1"'+((bimTagStyle == 0)?' /':'')+endTagSym); 
	} else {
		if(bimTagStyle == 2) r = r.replace(re,beginTagSym+'img'+endTagSym+'$1'+beginTagSym+'/img'+endTagSym); else r = r.replace(re,beginTagSym+'img=$1'+endTagSym); 
	}
    
    // block level formatting

	// BIM: don't really know what is this... Will try later!
	// Only a WORKAROUND!
	if(bimTagStyle == 0 || bimTagStyle == 1) {
		// Jeff's hack to show single line breaks as they should.
		// insert breaks - but you get some....stupid ones
	    re = new RegExp('(.*)\n([^#\*\n].*)','g');
	    r = r.replace(re,'$1<br />$2');
		// remove the stupid breaks.
	    re = new RegExp('\n<br />','g');
	    r = r.replace(re,'\n');
	} else {
	    re = new RegExp('(.*)\n([^#\*\n].*)','g');
	    r = r.replace(re,'$1<wasabr />$2');
		// remove the stupid breaks.
	    re = new RegExp('\n<wasabr />','g');
	    r = r.replace(re,'\n');
	}
	
    lines = r.split('\n');
    nr = '';
	var blockquoteTag = ((bimTagStyle == 0 || bimTagStyle == 1) ? 'blockquote' : 'quote'); // WORKAROUND (X)HTML uses cite, bbCode uses b
    for (var i=0;i<lines.length;i++) {
        line = lines[i].replace(/\s*$/,'');
        changed = 0;
        if (line.search(/^\s*bq\.\s+/) != -1) { line = line.replace(/^\s*bq\.\s+/,'\t'+beginTagSym+blockquoteTag+endTagSym)+beginTagSym+'/'+blockquoteTag+endTagSym; changed = 1; }
		
		// jeff adds h#.
        if (line.search(/^\s*h[1-6]\.\s+/) != -1) { 
	    	re = new RegExp('h([1-6])\.(.+)','g');
	    	line = line.replace(re,beginTagSym+'h$1'+endTagSym+'$2'+beginTagSym+'/h$1'+endTagSym);
			changed = 1; 
		}
		
		if (line.search(/^\s*\*\s+/) != -1) { line = line.replace(/^\s*\*\s+/,'\t'+beginTagSym+'liu'+endTagSym) + beginTagSym+'/liu'+endTagSym; changed = 1; } // * for bullet list; make up an liu tag to be fixed later
		if (line.search(/^\s*#\s+/) != -1) { line = line.replace(/^\s*#\s+/,'\t'+beginTagSym+'lio'+endTagSym) + beginTagSym+'/lio'+endTagSym; changed = 1; } // # for numeric list; make up an lio tag to be fixed later
		if(bimTagStyle == 0 || bimTagStyle == 1) {
			if (!changed && (line.replace(/\s/g,'').length > 0)) line = '<p>'+line+'</p>';
		}
        lines[i] = line + '\n';
    }
	
    // Second pass to do lists
    inlist = 0; 
	listtype = '';
    for (var i=0;i<lines.length;i++) {
        line = lines[i];
        if (inlist && listtype == 'ul' && !line.match(/^\t[<|\]]liu/)) { line = beginTagSym+'/ul'+endTagSym+'\n' + line; inlist = 0; }
        if (inlist && listtype == 'ol' && !line.match(/^\t[<|\]]lio/)) { line = beginTagSym+'/ol'+endTagSym+'\n' + line; inlist = 0; }
        if (!inlist && line.match(/^\t[<|\]]liu/)) { line = beginTagSym+'ul'+endTagSym + line; inlist = 1; listtype = 'ul'; }
        if (!inlist && line.match(/^\t[<|\]]lio/)) { line = beginTagSym+'ol'+endTagSym + line; inlist = 1; listtype = 'ol'; }
        lines[i] = line;
    }

    r = lines.join('\n');
	// jeff added : will correctly replace <li(o|u)> AND </li(o|u)>
    r = r.replace(/li[o|u][>|\]]/g,'li'+endTagSym);

	if (!(bimTagStyle == 0 || bimTagStyle == 1)) {
		r = r.replace(/<wasabr \/>/g, '\n');
	}
	
    return r;
}

function reloadPreviewDiv() { 
    var commentString = document.getElementById('message').value;
	var con = superTextile(commentString);
    document.getElementById('previewcomment').innerHTML = con;
}